package 蓝桥杯算法;

public class day29 {
    public static void main(String[] args) {
        day29 a=new day29();
        String s="rabbbit";
        String t="rabbit";
        System.out.println(a.childrennum(s,t));
        String s1="sea";
        String s2="eat";
        System.out.println(a.delete(s1,s2));
        String s3="intention";
        String s4="execution";
        System.out.println(a.change(s3,s4));

    }
    public int childrennum(String str1,String str2){
        int[][] dp=new int[str1.length()+1][str2.length()+1];
        for(int i=0;i< str1.length();i++){
            dp[i][0]=1;
        }
        for(int i=1;i<=str1.length();i++){
            for(int j=1;j<=str2.length();j++){
                if(str1.charAt(i-1)==str2.charAt(j-1)){
                    dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
                }
                else{
                    dp[i][j]=dp[i-1][j];
                }
            }
        }
        return dp[str1.length()][str2.length()];
    }
    public int delete(String s1,String s2){
        //数组dp从11开始，没有相关的00的值
        int[][]dp=new int[s1.length()+1][s2.length()+1];
        for(int i=1;i<=s1.length();i++){
            for(int j=1;j<=s2.length();j++){
                if(s1.charAt(i-1)==s2.charAt(j-1)){
                    dp[i][j]=dp[i-1][j-1]+1;
                }
                else{
                    dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }

        int res=dp[s1.length()][s2.length()];
        return res;
    }
    public int change(String s1,String s2){
        int[][]dp=new int[s1.length()+1][s2.length()+1];
       for(int i=1;i<=s1.length();i++){
           dp[i][0]=i;
       }
       for(int j=1;j<=s2.length();j++){
           dp[0][j]=j;
       }
       for(int i=1;i<=s1.length();i++){
           for(int j=1;j<=s2.length();j++){
               if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
                   dp[i][j] = dp[i - 1][j - 1];
               } else {
                   dp[i][j] = Math.min(Math.min(dp[i - 1][j - 1], dp[i][j - 1]), dp[i - 1][j]) + 1;
               }
           }
       }
       return dp[s1.length()][s2.length()];
    }
}
